﻿using System;

namespace IGS.Core
{
    public struct Vector
    {
        public double X;
        public double Y;
        public double Z;

        public double Length
        {
            get { return Math.Sqrt(X * X + Y * Y + Z * Z); }
        }

        public Vector(double x, double y, double z)
        {
            X = x;
            Y = y;
            Z = z;
        }

        public static Vector operator +(Vector a, Vector b)
        {
            return new Vector(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
        }

        public static Vector operator -(Vector a, Vector b)
        {
            return new Vector(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
        }

        public static Vector operator *(double s, Vector v)
        {
            return new Vector(s * v.X, s * v.Y, s * v.Z);
        }

        public override string ToString()
        {
            return String.Format("{0:0.000} {1:0.000} {2:0.000}", X, Y, Z);
        }

#pragma warning disable 659
        public override bool Equals(object obj)
#pragma warning restore 659
        {
            if (ReferenceEquals(null, obj)) return false;
            return obj is Vector && Equals((Vector) obj);
        }

        public bool Equals(Vector other)
        {
            const double epsilon = double.Epsilon;
            return (Math.Abs(X - other.X) < epsilon) && (Math.Abs(Y - other.Y) < epsilon) &&
                   (Math.Abs(Z - other.Z) < epsilon);
        }
    }
}